import {Component, OnInit, Input, OnChanges, TemplateRef, ViewChild} from '@angular/core';
import * as echarts from 'echarts';
import {NzMessageService} from "ng-zorro-antd";
import {FetchService} from "../../../core/net/fetch.service";
import {DomSanitizer, SafeResourceUrl} from "@angular/platform-browser";
import {format} from "date-fns";
import {NzModalRef, NzModalService} from "ng-zorro-antd/modal";
import {SwiperComponent, SwiperConfigInterface, SwiperDirective} from "ngx-swiper-wrapper";

const irrigateProcessHash = {
  '红寺堡扬水': 81,
  '盐环定扬水': 78,
  '固海扬水': 84,
  '中卫各渠': 87,
  '跃进渠': 75,
  '七星渠': 87,
  '秦汉渠': 77,
  '渠首': 75,
  '西干渠': 71,
  '汉延渠': 65,
  '惠农渠': 55,
  '唐徕渠': 73,
};

@Component({
  selector: "app-zx-panel",
  templateUrl: "./zx-panel.component.html",
  styleUrls: ["./zx-panel.component.scss"]
})
export class ZxPanelComponent implements OnInit {
  array = [1, 2, 3, 4];
  jrsqData = [];
  ggjdData = {};
  waterData = {};

  warningList: any[] = [];

  ggjdModalShow = false;
  ggjdReportSrc: SafeResourceUrl;
  ggjdReportType = 1;

  jrsqModalShow = false;
  jrsqReportSrc: SafeResourceUrl;
  jrsqType = 1;

  public config: SwiperConfigInterface = {
    a11y: true,
    direction: 'horizontal',
    slidesPerView: 1,
    keyboard: true,
    mousewheel: true,
    scrollbar: false,
    navigation: true,
    pagination: false
  };

  index = 0;
  @ViewChild(SwiperComponent, {static: false}) componentRef?: SwiperComponent;
  @ViewChild(SwiperDirective, {static: false}) directiveRef?: SwiperDirective;
  tplModal: NzModalRef;

  constructor(
    private message: NzMessageService,
    private fetchService: FetchService,
    private sanitizer: DomSanitizer,
    private modal: NzModalService
  ) {
  }

  ngOnInit() {
    this.queryInfo();
    this.queryGqjjReport();
    this.queryJrsqReport();

  /*  setInterval(() => {
      this.waterData['planVal']++;
    }, 3000);*/
  }

  queryInfo(): void {
    // 今日水情
    this.fetchService.post<any>('muw/wrsMuwWatReg/list', {}).subscribe(res => {
      this.jrsqData = res;
    });

    // 灌溉进度
    this.fetchService.post<any>('muw/wrsMuwIrrData/statisticArea', {}).subscribe(res => {
      res.map(item => {
        this.ggjdData[item.name] = item.val;
      });
    });

    // 引水量
    this.fetchService.post<any>('muw/wrsMuwIrrData/statisticWater', {}).subscribe(res => {
      console.log(res);
      this.waterData = res;
    });

    // 警情数据
    this.fetchService.post<any>('schedule/actualDispatchWarning/list', {
      pageSize: 1000,
      orderByField: "ctDate",
      orderBy: "desc "
    }).subscribe(res => {
      this.warningList = res.list;
    });
  }


  queryGqjjReport(): void {
    const year = new Date().getFullYear();
    const month = new Date().getMonth() + 1;
    const token = localStorage.token;
    const serverUrl = (window as any).environment.reportUrl;
    let link = '';
    if (this.ggjdReportType == 1) {
      link = `${serverUrl}preview?_u=wrs:实时调度_引水量.ureport.xml&_t=3,6&year=${year}&month=${month}&token=${token}`;
    }
    if (this.ggjdReportType == 2) {
      link = `${serverUrl}preview?_u=wrs:实时调度_市县用水量.ureport.xml&_t=3,6&year=${year}&month=${month}&token=${token}`;
    }
    this.ggjdReportSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  queryJrsqReport(): void {
    const token = localStorage.token;
    const date = format(new Date(), 'YYYY-MM-DD');
    const serverUrl = (window as any).environment.reportUrl;
    const link = `${serverUrl}preview?_u=wrs:今日水情.ureport.xml&_t=3,6&date=${date}&token=${token}`;
    this.jrsqReportSrc = this.sanitizer.bypassSecurityTrustResourceUrl(link);
  }

  showModal(tplContent: TemplateRef<{}>): void {
    const that = this;
    this.tplModal = this.modal.create({
      nzTitle: '今日水情',
      nzContent: tplContent,
      nzClassName: 'dispatch-modal',
      nzWidth: '96%',
      nzFooter: null,
      nzMaskClosable: false,
      nzStyle: {top: '40px'},
      nzClosable: true,
      nzOnCancel(): void {
        that.destroyModal();
      }
    });

  }

  destroyModal() {
    this.tplModal.destroy();
  }

}
